package com.tilaiedu.mvc.common;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author xlz
 * @date 2022/2/26 9:24
 * 返回值类型的固定成员：
 *    Integer  code   业务状态码，跟HTTP协议状态码不同，是对HTTP协议状态码的进一步细分
 *    String  message  要返回的简单的字符串信息
 *    Object  data    其他的复杂数据类型
 */
@Data
@Accessors(chain = true)  // 开启链式编程语法
public class R<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    // 屏蔽默认构造函数，让外界不不能直接创建对象
    private R(){}

    public static<T> R<T> ok(){   // success
        final R<T> r = new R<>();
        r.code = ResultCode.SUCCESS.code;
        r.message = ResultCode.SUCCESS.message;
        return r;
    }

    public static<T> R<T> ok(T data){
        final R<T> r = new R<>();
        r.code = ResultCode.SUCCESS.code;
        r.message = ResultCode.SUCCESS.message;
        r.data = data;
        return r;
    }

    public static<T> R<T> ok(ResultCode resultCode, T data){
        final R<T> r = new R<>();
        r.code = resultCode.code;
        r.message = resultCode.message;
        r.data = data;
        return r;
    }

    public static<T> R<T> ok(String message, T data){
        final R<T> r = new R<>();
        r.code = ResultCode.SUCCESS.code;
        r.message = message;
        r.data = data;
        return r;
    }

    public static<T> R<T> error(){
        final R<T> r = new R<>();
        r.code = ResultCode.UNKNOWN.code;
        r.message = ResultCode.UNKNOWN.message;
        return r;
    }

    public static<T> R<T> error(String message){
        final R<T> r = new R<>();
        r.code = ResultCode.UNKNOWN.code;
        r.message = message;
        return r;
    }

    public static<T> R<T> error(ResultCode resultCode){
        final R<T> r = new R<>();
        r.code = resultCode.code;
        r.message = resultCode.message;
        return r;
    }

    public static<T> R<T> error(ResultCode resultCode,T data){
        final R<T> r = new R<>();
        r.code = resultCode.code;
        r.message = resultCode.message;
        r.data = data;
        return r;
    }
}